var gulp = require('gulp'),  
    $ = require('gulp-load-plugins')(),
    option={
        buildPath:"./dist/"
    },
    buildFlag = false,
    px2rem = require("postcss-px2rem"),
    gulpSequence = require('gulp-sequence');

gulp.task('clean', function() {  
    return gulp.src(option.buildPath, {read: false})
        .pipe($.clean({force:true}));
});
gulp.task('html',function() {
    gulp.src(['./src/html/**/*.html','!./src/html/**/_*.html'])
    .pipe($.fileInclude({
        prefix: '@@',
        basepath: '@file'
    }))
    .pipe(gulp.dest(option.buildPath));
});
gulp.task('less', function() {
    return gulp.src(['./src/less/**/*.less','./src/less/**/*.css', '!./src/less/**/_*.less','!./src/less/**/*_mobile.less'])
        .pipe($.less())
        .pipe($.if(buildFlag,$.minifyCss()))
        .pipe(gulp.dest(option.buildPath + '/css/'))
});
gulp.task('lessM', function() {
    return gulp.src(['./src/less/**/*_mobile.less'])
        .pipe($.less())
        // .pipe($.postcss([px2rem({remUnit: 37.5})]))      
        .pipe($.if(buildFlag,$.postcss([px2rem({remUnit: 37.5})])))
        .pipe($.if(buildFlag,$.minifyCss()))
        .pipe(gulp.dest(option.buildPath + '/css/'))
});
gulp.task('js',function(){
    gulp.src(['./src/js/**/*.js','!./src/js/**/_*.js'])
    .pipe($.jshint())
    .pipe($.fileInclude({
            prefix: '@@',
            basepath: '@file'
        }))
    .pipe($.if(buildFlag,$.uglify()))
    .pipe(gulp.dest(option.buildPath + '/js/'))
});
gulp.task('lib',function(){
    gulp.src('./src/lib/**/*')
    .pipe(gulp.dest(option.buildPath + '/lib/'));
});
gulp.task('font',function(){
    gulp.src('./src/font/**/*')
    .pipe(gulp.dest(option.buildPath + '/font/'));
});

gulp.task('copyImages',function(){
    gulp.src('./src/images/**/*')
    .pipe(gulp.dest(option.buildPath + '/images/'));
});

gulp.task('watch', function() {
    gulp.watch('./src/less/**/*', ['less','lessM']);  
    gulp.watch('./src/js/**/*', ['js']);  
    gulp.watch('./src/html/**/*.html', ['html']);
});

gulp.task('default',['clean'],function(){
    gulp.start('less','lessM','js','lib','copyImages','font','html','watch');
});

gulp.task('start',['clean'],function(){
    gulp.start(gulpSequence('js','less',function(){
        gulp.start('copyImages','font','html');
    }))
});

gulp.task('release',['clean'],function(){
    buildFlag = true;
    gulp.start(gulpSequence('js','lib','less','lessM',function(){
        gulp.start('copyImages','font','html');
    }))
});




